<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        //实践：使用生成器函数实现异步编程，1s后在控制台打印111，再过2s后在打印222，再过3s后打印333

        //老方法，回调地狱
        // setTimeout(()=>{
        //     console.log(111)
        //     setTimeout(()=>{
        //         console.log(222)
        //         setTimeout(()=>{
        //             console.log(333)
        //         }, 3000)
        //     }, 2000)
        // }, 1000)

        //使用生成器函数
        function * gen() {
            yield delayPrint(111, 1000)
            yield delayPrint(222, 2000)
            yield delayPrint(333, 3000)
        }

        function delayPrint(val, milis) {
            setTimeout(()=>{
                console.log(val)
                it.next() //在setTimeout回调中执行迭代器next
            }, milis)
        }

        let it = gen()
        it.next()

    </script>
</body>
</html>